# -*- coding: utf-8 -*-
# coding=utf-8
'''
@author: Administrator
'''
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)


import codecs
import csv
import DBCenter


'''
    导入warehouse
'''

def RunCheck():
    csvfile = open('warehouse.csv', 'r')
    reader = csv.reader(csvfile)
    for row in reader:
        insert_sql = '11'
#         try:
        
 
        
        province_data = DBCenter.rc_db.get("SELECT * FROM province WHERE province_name LIKE '%%" + row[2] + "%%'");
        print row[3],province_data
        city_data = DBCenter.rc_db.get("SELECT * FROM city WHERE father = '" + province_data.province_id + "' AND city_name LIKE '%%" + row[3] + "%%'");
        district_data = DBCenter.rc_db.get("SELECT * FROM district WHERE father = '" + city_data.city_id + "' AND district_name LIKE '%%" + row[4] + "%%'");
        
        insert_sql = '''
            INSERT INTO `warehouse`
                        (`warehouse_id`,
                         `warehouse_name`,
                         `province_id`,
                         `province_name`,
                         `city_id`,
                         `city_name`,
                         `district_id`,
                         `district_name`,
                         `address`,
                         `contacter`,
                         `cellphone`,
                         `qq`,
                         `remark`,
                         `status`)
            VALUES (%s,
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    '%s',
                    1);
        ''' % (row[0], 
               row[1], 
               province_data.province_id,
               province_data.province_name,
               city_data.city_id,
               city_data.city_name,
               district_data.district_id,
               district_data.district_name,
               row[8],
               row[5],
               row[6], 
               row[7], 
               row[9],
               )
        
        print insert_sql
    
        DBCenter.rc_db.execute(insert_sql)
#         except:
#             print insert_sql
            
def Run():
    RunCheck()
    
Run()
# print os.getcwd()



